home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / ZMODEM / SZ.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  45KB  |  2,123 lines

  1. #define    __TOWNS__
  2. //#define    DEBUG
  3. #define VERSION "3.11 02-26-91"
  4. #define PUBDIR "/usr/spool/uucppublic"
  5.  
  6. #ifdef    __TOWNS__
  7. #    define    TXBSIZE    16384
  8. #    define    READCHECK
  9. #endif
  10.  
  11. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  12.  
  13. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  14.  
  15.  ****************************************************************************
  16.  *
  17.  * sz.c By Chuck Forsberg,  Omen Technology INC
  18.  *
  19.  ****************************************************************************
  20.  *
  21.  * Typical Unix/Xenix/Clone compiles:
  22.  *
  23.  *        cc -O sz.c -o sz                USG (SYS III/V) Unix
  24.  *        cc -O -DSV sz.c -o sz            Sys V Release 2 with non-blocking input
  25.  *                                        Define to allow reverse channel checking
  26.  *        cc -O -DV7  sz.c -o sz            Unix Version 7, 2.8 - 4.3 BSD
  27.  *
  28.  *        cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz        Classic Xenix
  29.  *
  30.  *        ln sz sb                        **** All versions ****
  31.  *        ln sz sx                        **** All versions ****
  32.  *
  33.  ****************************************************************************
  34.  ****************************************************************************
  35.  *
  36.  *
  37.  * A program for Unix to send files and commands to computers running
  38.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  39.  *
  40.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  41.  *
  42.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  43.  *
  44.  * 
  45.  *        This version implements numerous enhancements including ZMODEM
  46.  *        Run Length Encoding and variable length headers.  These
  47.  *        features were not funded by the original Telenet development
  48.  *        contract.
  49.  * 
  50.  * This software may be freely used for non commercial and
  51.  * educational (didactic only) purposes.  This software may also
  52.  * be freely used to support file transfer operations to or from
  53.  * licensed Omen Technology products.  Any programs which use
  54.  * part or all of this software must be provided in source form
  55.  * with this notice intact except by written permission from Omen
  56.  * Technology Incorporated.
  57.  * 
  58.  * Use of this software for commercial or administrative purposes
  59.  * except when exclusively limited to interfacing Omen Technology
  60.  * products requires a per port license payment of $20.00 US per
  61.  * port (less in quantity).  Use of this code by inclusion,
  62.  * decompilation, reverse engineering or any other means
  63.  * constitutes agreement to these conditions and acceptance of
  64.  * liability to license the materials and payment of reasonable
  65.  * legal costs necessary to enforce this license agreement.
  66.  *
  67.  *
  68.  *                Omen Technology Inc                FAX: 503-621-3745
  69.  *                Post Office Box 4681
  70.  *                Portland OR 97208
  71.  *
  72.  *        This code is made available in the hope it will be useful,
  73.  *        BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  74.  *        DAMAGES OF ANY KIND.
  75.  */
  76.  
  77.  
  78. #ifndef    __TOWNS__
  79. char       *substr(), *getenv();
  80. #endif
  81.  
  82. #define LOGFILE "/tmp/szlog"
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <signal.h>
  87. #include <stat.h>
  88. #include <setjmp.h>
  89. #include <ctype.h>
  90. #include <errno.h>
  91.  
  92. #ifdef    __TOWNS__
  93. #    include    <stdarg.h>
  94. #    include    <process.h>
  95. #    include    <time.h>
  96. #    include    <io.h>
  97. #    include    <splib.h>
  98. #    include    <fslib.h>
  99.  
  100. #    include    "usrlib.h"
  101. #    include    "rsctrl.h"
  102. #    include    "flib.h"
  103. #    include    "msgdat.h"
  104.  
  105. #    ifdef    __HIGHC__
  106. #        pragma    On(Align_labels);
  107. #    endif
  108.  
  109.     extern    int    RsPort;
  110. #    define    _FSTAT_IGN
  111. #endif
  112.  
  113. #include    "prot.h"
  114. #include    "sz.h"
  115.  
  116. extern    int        errno;
  117. #define    STATIC    static
  118.  
  119. #define HOWMANY 2
  120. STATIC int        Zmodem = 0;         /* ZMODEM protocol requested by receiver */
  121. unsigned        Baudrate = 4800;    /* Default, set by first mode() call */
  122. unsigned        Effbaud = 4800;
  123. STATIC unsigned Txwindow;            /* Control the size of the transmitted window */
  124. STATIC unsigned Txwspac;            /* Spacing between zcrcq requests */
  125. STATIC unsigned Txwcnt;             /* Counter used to space ack requests */
  126. STATIC long        Lrxpos;             /* Receiver's last reported offset */
  127. STATIC int        errors;
  128. char            endmsg[80] = {0};    /* Possible message to display on exit */
  129.  
  130. #include "rbsb.c"                    /* most of the system dependent stuff here */
  131. #include "crctab.c"
  132.  
  133. STATIC int    Filesleft;
  134. STATIC long    Totalleft;
  135.  
  136. /*
  137.  * Attention string to be executed by receiver to interrupt streaming data
  138.  *  when an error is detected.  A pause (0336) may be needed before the
  139.  *  ^C (03) or after it.
  140.  */
  141. #ifdef READCHECK
  142.     STATIC char Myattn[] = {0};
  143. #else
  144. #    ifdef USG
  145.         STATIC char Myattn[] = {03, 0336, 0};
  146. #    endif
  147. #endif
  148.  
  149. FILE       *in;
  150. char       *infile;    /* High C stat() */
  151.  
  152. #ifdef BADSEEK
  153. STATIC int    Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  154. #    ifndef TXBSIZE
  155. #        define TXBSIZE 16384    /* Must be power of two, < MAXINT */
  156. #    endif
  157. #else
  158. STATIC int    Canseek = 1;        /* 1: Can seek 0: only rewind -1: neither (pipe) */
  159. #endif
  160.  
  161. #ifdef TXBSIZE
  162. #define TXBMASK (TXBSIZE-1)
  163. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  164. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  165. #else
  166. STATIC char txbuf[1024];
  167. #endif
  168. STATIC long vpos = 0;            /* Number of bytes read from file */
  169.  
  170. STATIC char Lastrx;
  171. STATIC char Crcflg;
  172. #ifdef    DEBUG
  173. STATIC int    Verbose = 5;
  174. #else
  175. STATIC int    Verbose = 0;
  176. #endif
  177. STATIC int    Modem2 = 0;         /* XMODEM Protocol - don't send pathnames */
  178. STATIC int    Restricted = 0;     /* restricted; no /.. or ../ in filenames */
  179. STATIC int    Quiet = 0;            /* overrides logic that would otherwise set verbose */
  180. STATIC int    Ascii = 0;            /* Add CR's for brain damaged programs */
  181. STATIC int    Fullname = 0;        /* transmit full pathname */
  182. STATIC int    Unlinkafter = 0;    /* Unlink file after it is sent */
  183. STATIC int    Dottoslash = 0;     /* Change foo.bar.baz to foo/bar/baz */
  184. STATIC int    firstsec;
  185. STATIC int    errcnt = 0;         /* number of files unreadable */
  186. STATIC int    blklen = 128;        /* length of transmitted records */
  187. STATIC int    Optiong;            /* Let it rip no wait for sector ACK's */
  188. STATIC int    Eofseen;            /* EOF seen on input set by zfilbuf */
  189. STATIC int    BEofseen;            /* EOF seen on input set by fooseek */
  190. STATIC int    Totsecs;            /* total number of sectors this file */
  191. STATIC int    Filcnt = 0;         /* count of number of files opened */
  192. STATIC int    Lfseen = 0;
  193. STATIC unsigned Rxbuflen = 16384;    /* Receiver's max buffer length */
  194. STATIC int    Tframlen = 0;        /* Override for tx frame length */
  195. STATIC int    blkopt = 0;         /* Override value for zmodem blklen */
  196. STATIC int    Rxflags = 0;
  197. STATIC long bytcnt;
  198. STATIC int    Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  199. STATIC char Lzconv;             /* Local ZMODEM file conversion request */
  200. STATIC char Lzmanag;            /* Local ZMODEM file management request */
  201. STATIC int    Lskipnocor;
  202. STATIC char Lztrans;
  203. STATIC int    Command;            /* Send a command, then exit. */
  204. STATIC char *Cmdstr;            /* Pointer to the command string */
  205. STATIC int    Cmdtries = 11;
  206. STATIC int    Cmdack1;            /* Rx ACKs command, then do it */
  207. STATIC int    Exitcode;
  208. STATIC int    Test = 0;            /* 1= Force receiver to send Attn, etc with qbf.
  209.                                    2= Character transparency test */
  210. STATIC char *qbf = "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  211. STATIC long Lastsync = 0;        /* Last offset to which we got a ZRPOS */
  212. STATIC int    Beenhereb4 = 0;     /* How many times we've been ZRPOS'd same place */
  213.  
  214. STATIC jmp_buf tohere;            /* For the interrupt on RX timeout */
  215. STATIC jmp_buf intrjmp;         /* For the interrupt on RX CAN */
  216.  
  217. #ifdef XARGSFILE
  218. static    char       *mystrsave(char *s)
  219. {
  220.     register char *p;
  221.     char       *malloc();
  222.  
  223.     if (p = malloc(strlen(s) + 1))
  224.     {
  225.         strcpy(p, s);
  226.         return p;
  227.     }
  228.     USR_fprintf(stderr, "No memory for mystrsave!\n");
  229.     EXIT(1);
  230. }
  231.  
  232. /* Remove (presumably) terminating CR and/or LF from string */
  233. static    void    uncrlf( register char *s )
  234. {
  235.     for (; *s; ++s)
  236.         switch (*s)
  237.         {
  238.             case '\r':
  239.             case '\n':
  240.                 *s = 0;
  241.                 return;
  242.         }
  243. }
  244. #endif
  245.  
  246.  
  247. #ifdef    __TOWNS__
  248.  
  249. void    (*signal_int_func)(int) = SIG_DFL;
  250. extern    int        CtrlX;
  251.  
  252. void    signal_func(int level)
  253. {
  254.     if ( signal_int_func == SIG_ERR )
  255.     {
  256.     } else if ( signal_int_func == SIG_DFL )
  257.     {
  258.         EXIT(3);
  259.     } else if ( signal_int_fu